package com.dominator.utils.sms;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dominFramework.core.typewrap.Dto;
import com.dominator.utils.system.Constants;
import com.dominator.utils.network.HttpPoster;
import com.dominator.utils.system.PropertiesLoader;
import com.dominator.utils.system.VerifyCodeUtil;
import com.dominator.utils.system.XmlConverUtil;
import com.dominator.utils.exception.ApiException;
/**
 * 龙举短信发送
 * @author Administrator
 *
 */
public class LongJuSmsUtils {

	public static PropertiesLoader propertiesLoader = new PropertiesLoader("sysconfig.properties");
    /**
     * 发送短信
     * @param phoneNum
     * @param verifyCode
     * @param type
     * @param dto
     * @throws ApiException
     */
	public static void sendSms(Dto dto) throws ApiException {
		System.out.println("longju发送短信开始");
		String content = "";
		switch (dto.getInteger("stype")) {
		case 1:
			content = propertiesLoader.getProperty("sms.notice.register_tpl");
			content = String.format(content, dto.getInteger("verifyCode"));
			break;
		case 2:
			content = propertiesLoader.getProperty("sms.notice.login_tpl");
			content = String.format(content, dto.getInteger("verifyCode"));
			break;
		case 3:
			content = propertiesLoader.getProperty("sms.notice.find_login_tpl");
			content = String.format(content, dto.getInteger("verifyCode"));
			break;
		case 4:
			content = propertiesLoader.getProperty("sms.notice.recharge_tpl");
			try {
				content = String.format(content, dto.getString("phoneNum"),dto.getString("payMoney"),new String(dto.getString("lesson").getBytes("utf-8"), "ISO-8859-1"));
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				throw new ApiException(Constants.SYS_ERROR, "模版编码错误");
			}
			break;
		case 5:
			content = propertiesLoader.getProperty("sms.notice.change_bean_tpl");
			content = String.format(content, dto.getString("phoneNum"),dto.getString("payMoney"),dto.getString("studyBean"),dto.getString("profit"));
			break;
		case 6:
			content = propertiesLoader.getProperty("sms.notice.order_bean_tpl");
			content = String.format(content, dto.getString("phoneNum"),dto.getString("lesson"),dto.getString("usedStudyBean"),dto.getString("totalStudyBean"));
			break;
		}

		try {
			content =propertiesLoader.getProperty("sms.notice.sign")+content+propertiesLoader.getProperty("sms.notice.tail_tpl");
			System.out.println(content);
			content = new String(content.getBytes("ISO-8859-1"), "utf-8");
			System.out.println(content);
		} catch (UnsupportedEncodingException e) {
			throw new ApiException(Constants.SYS_ERROR, "模版编码错误");
		}
		content = String.format(content, VerifyCodeUtil.getCode6());
		System.out.println(content);
		Map<String, String> params = new HashMap<String, String>();
		params.put("action", "send");
		params.put("userid", "63");
		params.put("account", propertiesLoader.getProperty("sms.longju.user_code"));
		params.put("password", propertiesLoader.getProperty("sms.longju.user_pass"));
		params.put("sendTime", "");
		params.put("extno", "");
		params.put("mobile", dto.getString("phoneNum"));
		params.put("content", content);
		String res = null;
		System.out.println("longju开始发送短信");
		try {
			res = HttpPoster.post(propertiesLoader.getProperty("sms.longju.url"), params);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApiException(Constants.SYS_ERROR, "发送请求失败");
		}
		System.out.println("longju发送短信结束");

		System.out.println(res);
		String jsonStr = XmlConverUtil.xmltoJson(res);
		System.out.println(jsonStr);
		JSONObject json = (JSONObject) JSON.parse(jsonStr);
		if ("Success".equals(json.getString("returnstatus"))) {
			if ("ok".equals(json.getString("message"))) {
				System.out.println("发送成功");
			} else {
				System.out.println("发送失败！原因： " + json.getString("message"));
				throw new ApiException(Constants.SYS_ERROR, json.getString("message"));
			}
		} else {
			System.out.println("调用短信接口失败");
			throw new ApiException(Constants.SYS_ERROR, "调用短信接口失败");
		}

	}

	
	
}
